
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
import javax.swing.text.StyleConstants;
import javax.xml.soap.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/**
 * 系统主页面的操作
 */


public class mySystemJFrame extends JFrame implements ActionListener {
    //显示如下功能菜单：输入数据、删除信息、修改信息、查询信息；
    private JComboBox insert,delete,update,search;
    private int section;  //用于功能标识
    private JTable jtable;//用于做查询结果的表格
    private Font x = new Font("宋体",Font.BOLD,20);  //文本字体

    public mySystemJFrame(){
        super("王芯的数据库应用中心");
        Dimension dim = getToolkit().getScreenSize(); // 获得屏幕分辨率
        this.setBounds(dim.width/10,dim.height/10,dim.width *3/ 5, dim.height *3/ 5); // 窗口居中
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        ImageIcon background = new ImageIcon("src\\images\\bgSystem.png");
        JLabel label = new JLabel(background);
        label.setBounds(0, 0, this.getWidth(), this.getHeight());
        JPanel imagePanel = (JPanel) this.getContentPane();
        imagePanel.setOpaque(false);
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));


        JMenuBar menubar=new JMenuBar();                    //菜单栏
        this.setJMenuBar(menubar);                          //框架上添加菜单栏
        String menustr[]={"输入数据","删除信息","修改信息","查询信息"};
        JMenu menu[]=new JMenu[menustr.length];
        for (int i=0;i<menustr.length;i++){
            menu[i]=new JMenu(menustr[i]);                  //菜单
            menubar.add(menu[i]);                           //菜单栏中加入菜单
        }

        //输入菜单的添加
        ImageIcon icon1=new ImageIcon("src\\images\\btn1.png");
        JMenuItem menuItem_insert1 = new JMenuItem("输入学生档案信息",icon1);
        menu[0].add(menuItem_insert1);                      //生成菜单项并加入到菜单
        menu[0].addSeparator();                             //添加分隔线
        menuItem_insert1.addActionListener(this);
        JMenuItem menuItem_insert2 = new JMenuItem("输入课程信息",icon1);
        menu[0].add(menuItem_insert2);
        menu[0].addSeparator();
        menuItem_insert2.addActionListener(this);
        JMenuItem menuItem_insert3 = new JMenuItem("输入学生修课信息",icon1);
        menu[0].add(menuItem_insert3);
        menu[0].addSeparator();
        menuItem_insert3.addActionListener(this);

        //删除菜单的添加
        ImageIcon icon2=new ImageIcon("src\\images\\btn2.png");
        JMenuItem menuItem_delete1 = new JMenuItem("删除学生档案信息",icon2);
        menu[1].add(menuItem_delete1);                      //生成菜单项并加入到菜单
        menu[1].addSeparator();                             //添加分隔线
        menuItem_delete1.addActionListener(this);
        JMenuItem menuItem_delete2 = new JMenuItem("删除课程信息",icon2);
        menu[1].add(menuItem_delete2);
        menu[1].addSeparator();
        menuItem_delete2.addActionListener(this);
        JMenuItem menuItem_delete3 = new JMenuItem("删除学生修课信息",icon2);
        menu[1].add(menuItem_delete3);
        menu[1].addSeparator();
        menuItem_delete3.addActionListener(this);

        //修改菜单的添加
        ImageIcon icon3=new ImageIcon("src\\images\\btn3.png");
        JMenuItem menuItem_update1 = new JMenuItem("修改学生档案信息",icon3);
        menu[2].add(menuItem_update1);                      //生成菜单项并加入到菜单
        menu[2].addSeparator();                             //添加分隔线
        menuItem_update1.addActionListener(this);
        JMenuItem menuItem_update2 = new JMenuItem("修改课程信息",icon3);
        menu[2].add(menuItem_update2);
        menu[2].addSeparator();
        menuItem_update2.addActionListener(this);
        JMenuItem menuItem_update3 = new JMenuItem("修改学生修课信息",icon3);
        menu[2].add(menuItem_update3);
        menu[2].addSeparator();
        menuItem_update3.addActionListener(this);

        //查询菜单的添加
        ImageIcon icon4=new ImageIcon("src\\images\\btn4.png");
        JMenuItem menuItem_search1 = new JMenuItem("查询学生档案信息",icon4);
        menu[3].add(menuItem_search1);                      //生成菜单项并加入到菜单
        menu[3].addSeparator();                             //添加分隔线
        menuItem_search1.addActionListener(this);
        JMenuItem menuItem_search2 = new JMenuItem("查询课程信息",icon4);
        menu[3].add(menuItem_search2);
        menu[3].addSeparator();
        menuItem_search2.addActionListener(this);
        JMenuItem menuItem_search3 = new JMenuItem("查询学生修课信息",icon4);
        menu[3].add(menuItem_search3);
        menu[3].addSeparator();
        menuItem_search3.addActionListener(this);
        JMenuItem menuItem_search4 = new JMenuItem("查询指定学号的学生的全部信息",icon4);
        menu[3].add(menuItem_search4);
        menu[3].addSeparator();
        menuItem_search4.addActionListener(this);
        JMenuItem menuItem_search5 = new JMenuItem("查询指定课程所修学生的全部信息",icon4);
        menu[3].add(menuItem_search5);
        menu[3].addSeparator();
        menuItem_search5.addActionListener(this);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        //添加操作
        if(e.getActionCommand() == "输入学生档案信息"||e.getActionCommand() == "输入课程信息"||e.getActionCommand() == "输入学生修课信息"){
            if(e.getActionCommand() == "输入学生档案信息"){
                section = 1;
            }else if(e.getActionCommand() == "输入课程信息"){
                section = 2;
            }else {
                section = 3;
            }
            String datas = JOptionPane.showInputDialog(this,"请输入想添加的信息\n请用空格区分\nO(∩_∩)O哈哈~");
            dbFunction db = new dbFunction();
            boolean flag = db.zhw(datas,section);
            if(flag&&datas!=null){
                JOptionPane.showMessageDialog(this,"添加数据成功！");
            }else{
                JOptionPane.showMessageDialog(this,"添加数据失败！");
            }
        }

        //删除操作
        if(e.getActionCommand() == "删除学生档案信息"||e.getActionCommand() == "删除课程信息"||e.getActionCommand() == "删除学生修课信息"){
            String message="";
            if(e.getActionCommand() == "删除学生档案信息"){
                section = 4;
                message = "删除学生档案信息:主键为Sno\n";
            }else if(e.getActionCommand() == "删除课程信息"){
                section = 5;
                message = "删除课程信息:主键为Cno\n";
            }else {
                section = 6;
                message = "删除学生选课信息:主键为id\n";
            }
            String datas = JOptionPane.showInputDialog(this,"请输入想删除的信息\n" +message+"\n(*^__^*) 嘻嘻…");
            dbFunction db = new dbFunction();
            boolean flag = db.zhw(datas,section);
            if(flag&&datas!=null){
                JOptionPane.showMessageDialog(this,"删除数据成功！");
            }else{
                JOptionPane.showMessageDialog(this,"删除数据失败！");
            }
        }

        //修改操作
        if(e.getActionCommand() == "修改学生档案信息"||e.getActionCommand() == "修改课程信息"||e.getActionCommand() == "修改学生修课信息"){
            String message="";
            if(e.getActionCommand() == "修改学生档案信息"){
                section = 7;
                message = "修改学生档案信息:主键为Sno\n";
            }else if(e.getActionCommand() == "修改课程信息"){
                section = 8;
                message = "修改课程信息:主键为Cno\n";
            }else {
                section = 9;
                message = "修改学生选课信息:主键为id\n";
            }
            String datas = JOptionPane.showInputDialog(this,"请输入想修改的信息\n" +message+"\n请在要修改的信息前加上where进行区分\n(*@ο@*) 哇～");
            dbFunction db = new dbFunction();
            boolean flag = db.zhw(datas,section);
            if(flag&&datas!=null){
                JOptionPane.showMessageDialog(this,"修改数据成功！");
            }else{
                JOptionPane.showMessageDialog(this,"修改数据失败！");
            }
        }

        //查询操作
        if(e.getActionCommand() == "查询学生档案信息"||e.getActionCommand() == "查询课程信息"||e.getActionCommand() == "查询学生修课信息" ||e.getActionCommand() == "查询指定学号的学生的全部信息"||e.getActionCommand() == "查询指定课程所修学生的全部信息"){
            String datas=""; //直接查询所有信息
            if(e.getActionCommand() == "查询学生档案信息"){
                section = 10;
            }else if(e.getActionCommand() == "查询课程信息"){
                section = 11;
            }else if(e.getActionCommand() == "查询学生修课信息"){
                section = 12;
            }else if(e.getActionCommand() == "查询指定学号的学生的全部信息"){
                section = 13;
            }else{
                section = 14;
            }
            if(e.getActionCommand() == "查询指定学号的学生的全部信息"){
                datas = JOptionPane.showInputDialog(this,"请输入想查询的学生的学号 \n~~~~(>_<)~~~~");
            }else if(e.getActionCommand() == "查询指定课程所修学生的全部信息"){
                datas = JOptionPane.showInputDialog(this,"请输入想查询的课程的课程号 \n╭(╯^╰)╮");
            }
            dbFunction db = new dbFunction();
            try {
                jtable = db.query(section,datas);
                jtable.setRowHeight(30);
                jtable.setFont(x);
                if (this.getContentPane().getComponentCount() > 0)
                    this.getContentPane().remove(0);
                this.getContentPane().add(new JScrollPane(jtable));

                this.setVisible(true);
            } catch (SQLException e1) {
                //e1.printStackTrace();
            }

        }
    }
}
